import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_rider/main/widgets/dialog/alert_dialog.dart';
import 'package:flutter_rider/utils/log.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import '../api/api.dart';
import '../utils/tip.dart';

class AppUpdateKit {
  AppUpdateKit._privateConstructor();
  static final AppUpdateKit _instance = AppUpdateKit._privateConstructor();
  static AppUpdateKit get instance {
    return _instance;
  }

  check(BuildContext context, bool show) async {
    if (Platform.isIOS) {
      checkIos(context, show);
    } else if (Platform.isAndroid) {
      checkAndroid(context, show);
    }
  }

  checkIos(BuildContext context, bool show) async {
    // var pkgInfo = await PackageInfo.fromPlatform();
    // var url = "https://itunes.apple.com/CN/lookup?id=com.tencent.mm";
    // var dio = Dio();
    // try {
    //   var response = await dio.request(url);
    //   Log.i(response.data);
    // } on DioError catch(e) {
    //   Log.e(e.toString());
    // } catch (e) {
    //   Log.i(e);
    // }

    var pkgInfo = await PackageInfo.fromPlatform();
    Api.get("/app/new_version", (data, response) async {
      if (data != null) {
        // if (data["version_code"] <= int.parse(pkgInfo.buildNumber)) {
        var _sp = await SharedPreferences.getInstance();
        // 当前已是最新版本
        if (show) {
          CustomAlertDialog.showUpdateVersionDialog(
              context, {"currVersion": pkgInfo.version}, false,"客户端版本");
          _sp.setInt("account", 2);
        }
        int? account = _sp.getInt("account");
        if (account == 1) {
          CustomAlertDialog.showVersionUpdateDialog(context,
              {"newVersion": data["version_name"], "content": data["content"]});
          _sp.setInt("account", 2);
        }
        // } else {
        //   // 发现新版本，提示更新
        //   CustomAlertDialog.showUpdateVersionDialog(
        //       context,
        //       {
        //         "newVersion": data["version_name"],
        //         "oldVersion": pkgInfo.version,
        //         "dl_url": data["dl_url"]
        //       },
        //       true);
        // }
      } else {
        if (show) {
          Tip.show("检查更新失败");
        }
      }
    });
  }

  checkAndroid(BuildContext context, bool show) async {
    var pkgInfo = await PackageInfo.fromPlatform();
    Api.get("/app/new_version", (data, response) async {
      if (data != null) {
        if (data["version_code"] <= int.parse(pkgInfo.buildNumber)) {
          var _sp = await SharedPreferences.getInstance();
          // 当前已是最新版本
          if (show) {
            CustomAlertDialog.showUpdateVersionDialog(
                context, {"currVersion": pkgInfo.version}, false,"客户端版本");
            _sp.setInt("account", 2);
          }
          int? account = _sp.getInt("account");
          if (account == 1) {
            CustomAlertDialog.showVersionUpdateDialog(context, {
              "newVersion": data["version_name"],
              "content": data["content"]
            });
            _sp.setInt("account", 2);
          }
        } else {
          // 发现新版本，提示更新
          CustomAlertDialog.showUpdateVersionDialog(
              context,
              {
                "newVersion": data["version_name"],
                "oldVersion": pkgInfo.version,
                "dl_url": data["dl_url"]
              },
              true,
              "发现新版本");
        }
      } else {
        if (show) {
          Tip.show("检查更新失败");
        }
      }
    });
  }
}
